package cn.piaoyi.service.imp;

import java.util.*;

import org.apache.commons.logging.*;

import cn.piaoyi.bean.Cart;
import cn.piaoyi.bean.Daily;
import cn.piaoyi.bean.Goods;
import cn.piaoyi.bean.Price;
import cn.piaoyi.dao.CartDAO;
import cn.piaoyi.dao.DailyDAO;
import cn.piaoyi.dao.GoodsDAO;
import cn.piaoyi.dao.PriceDAO;
import cn.piaoyi.exception.PiaoyiException;
import cn.piaoyi.service.CartService;
import cn.piaoyi.service.PriceService;


public class CartServiceImp implements CartService {

	private static final Log logger = LogFactory.getLog(CartServiceImp.class);
	
	private CartDAO cartDAO;
	private GoodsDAO goodsDAO;
	private DailyDAO dailyDAO;
	private PriceDAO priceDAO;

	public CartDAO getCartDAO() {
		return cartDAO;
	}

	public void setCartDAO(CartDAO cartDAO) {
		this.cartDAO = cartDAO;
	}

	public GoodsDAO getGoodsDAO() {
		return goodsDAO;
	}

	public void setGoodsDAO(GoodsDAO goodsDAO) {
		this.goodsDAO = goodsDAO;
	}

	public DailyDAO getDailyDAO() {
		return dailyDAO;
	}

	public void setDailyDAO(DailyDAO dailyDAO) {
		this.dailyDAO = dailyDAO;
	}

	public PriceDAO getPriceDAO() {
		return priceDAO;
	}

	public void setPriceDAO(PriceDAO priceDAO) {
		this.priceDAO = priceDAO;
	}

	/**
	 * 保存Cart对象
	 * 
	 * @param cart
	 * 			Cart对象
	 * @return Cart
	 * 			Cart对象
	 */
	public Cart createCart(Cart cart) throws PiaoyiException{
		try {
			cart = this.getCartDAO().saveCart(cart);
			return cart;
		} catch (Exception e) {
			logger.error(e);
			throw new PiaoyiException(e);
		}
	}

	/**
	 * 保存Cart对象
	 * 
	 * @param cart
	 * 			Cart对象
	 * @return Cart
	 * 			Cart对象
	 */
	public Cart updateCart(Cart cart) throws PiaoyiException{
		try {
			cart = this.getCartDAO().updateCart(cart);
			return cart;
		} catch (Exception e) {
			logger.error(e);
			throw new PiaoyiException(e);
		}
	}
	
	/**
	 * 根据主键ID获取Cart对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return Cart
	 * 			Cart对象
	 */
	public Cart findCartById(long id){
		Cart c = this.getCartDAO().findCartById(id);
		if(c == null){
			return null;
		}else{
			Goods g = this.getGoodsDAO().findGoodsById(c.getGoodsId());
			Daily d = this.getDailyDAO().findDailyById(c.getDailyId());
			Price p = this.getPriceDAO().findPriceById(c.getPriceId());
			c.setGoods(g);
			c.setDaily(d);
			c.setPrice(p);
			c.setTotalPrice(c.getPrice().getPrice()*c.getGoodsNumber());
			return c;
		}
	}
	
	/**
	 * 根据UID PID获取Cart对象
	 * @param uid
	 * 			long
	 * @param pid
	 * 			long
	 * @return Cart
	 */
	public Cart findCartByUidPid(long uid,long pid){
		Cart c = this.getCartDAO().findCartByUidPid(uid, pid);
		if(c == null){
			return null;
		}else{
			Goods g = this.getGoodsDAO().findGoodsById(c.getGoodsId());
			Daily d = this.getDailyDAO().findDailyById(c.getDailyId());
			Price p = this.getPriceDAO().findPriceById(c.getPriceId());
			c.setGoods(g);
			c.setDaily(d);
			c.setPrice(p);
			c.setTotalPrice(c.getPrice().getPrice()*c.getGoodsNumber());
			return c;
		}
	}
	
	/**
	 * 获取Price列表
	 * 
	 * @param userId
	 * 			long
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @return List
	 * 			Price对象列表
	 */
	public List findCarts(long userId,String orderby, int ascOrDesc){
		List l = this.getCartDAO().findPrices(userId, orderby, ascOrDesc);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			for(int i=0;i<l.size();i++){
				Cart c = (Cart) l.get(i);
				Goods g = this.getGoodsDAO().findGoodsById(c.getGoodsId());
				Daily d = this.getDailyDAO().findDailyById(c.getDailyId());
				Price p = this.getPriceDAO().findPriceById(c.getPriceId());
				c.setGoods(g);
				c.setDaily(d);
				c.setPrice(p);
				c.setTotalPrice(c.getPrice().getPrice()*c.getGoodsNumber());
			}
			return l;
		}
	}
	
	/**
	 * 删除Cart对象
	 */
	public void removeCartById(long id){
		this.getCartDAO().removeCartById(id);
	}
	
	public void removeCartInIds(List ids){
		this.getCartDAO().removeCartInIds(ids);
	}
	
	public void removeCartByUid(long userId){
		this.getCartDAO().removeCartByUid(userId);
	}
	
	public void removeCartAll(){
		this.getCartDAO().removeCartAll();
	}
}
